package com.iotechn.unimall.core.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by rize on 2019/7/1.
 */
public class GeneratorUtil {

    private static AtomicInteger orderIdCount = new AtomicInteger();
    private static final SimpleDateFormat ORDER_ID_FORMAT = new SimpleDateFormat("yyyyMMHHmmss");

    private static AtomicInteger orderCodeCount = new AtomicInteger();
    private static final SimpleDateFormat ORDER_CODE_FORMAT = new SimpleDateFormat("yyyyMMdd");


    public static String genSixVerifyCode() {
        String time = System.nanoTime() + "";
        return time.substring(time.length() - 6);
    }

    public static String genSessionId() {
        return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }

    public synchronized static String getLangUUId() {
        return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }


    public static String genOrderId(String machineNo, String env) {
        int i = orderIdCount.incrementAndGet() % 1000;
        if (i < 1000)
            i += 1000;
        return env + machineNo + ORDER_ID_FORMAT.format(new Date()) + i;
    }

    public static String genCardOrderId(String machineNo, String env) {
        return env + machineNo + ORDER_ID_FORMAT.format(new Date()) + (int)((Math.random()*9+1)*1000);
    }

    /**
     * 根据时间获取唯一订单信息
     * @return
     */
    public static String getOrderIdByTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
          result+=random.nextInt(10);
        }
        return newDate+result;
    }

    public static String genFileName(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String genUUId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    //获取会员编码
    public static String genMemberCode(String machineNo, String env) {
        Integer arr[]=new Integer[]{10,11,12,13,14,15};
        int index=(int)(Math.random()*arr.length);
        int rand = arr[index];
        int i = orderIdCount.incrementAndGet() % 1000;
        if (i < 1000){
            i += 1000;}
        return 10 + ORDER_ID_FORMAT.format(new Date()) + i;
    }

    //获取快递取货码
    public static String genOrderCode() {
        int i = orderCodeCount.incrementAndGet() % 1000;
        if (i < 1000){i += 1000;}
        return 10 + ORDER_CODE_FORMAT.format(new Date()) + i;
    }

}
